home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10843 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: hubcap.clemson.edu!hubcap!mjs
  2. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: operator % - compiler error
  5. Date: 17 Mar 96 21:36:19 GMT
  6. Organization: Clemson University
  7. Message-ID: <mjs.827098579@hubcap>
  8. References: <4ihuuh$6ul@hatathli.csulb.edu>
  9. NNTP-Posting-Host: hubcap.clemson.edu
  10. X-Newsreader: NN version 6.5.0 #1
  11.  
  12. davidcho@csulb.edu (David Cho) writes:
  13.  
  14. >When I try to compile, I get an erro message for the following line:
  15.  
  16. >x=663608941*y%pow(2,32)  /*I want remainder*/
  17.  
  18. >But the error message says "illegal use of floating point".  What does 
  19. >that mean?  Isn't % used a an operator to calcuate the remainder?
  20.  
  21. Both operands of % must be integral types, but pow() returns a double
  22. (assuming you've #included <math.h>).  You didn't tell us if y is an
  23. int or not.
  24.  
  25. You have a couple of options:
  26.  
  27. (1) Do the entire compilation in doubles and use fmod() instead of %.
  28. Calculations involving exclusively integer operands should be done
  29. exactly if all intermediate results fit in the mantissa of a double
  30. (56 bits for IEEE doubles, I think).
  31.  
  32. (2) Find a way to construct 2 to the 32nd power (damn, it would be
  33. nice to have an acceptable C syntax for that--one always gets abuse in
  34. comp.lang.c for the common notations 2^32 or 2**32) in an integral
  35. type.  You could do this (with bit shifts repeated squares) if your
  36. architecture has 64-bit longs, but it's not portable.
  37.  
  38. (3) Rethink how you are doing this computation.  In 32-bit integers,
  39. you run the risk of overflow while computing the product anyway (if y
  40. is an int).
  41. -- 
  42.         Matthew Saltzman
  43.         Clemson University Math Sciences
  44.         mjs@clemson.edu
  45.